﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ShiftSchedulingManagementDto;
using ThridGroup.ERP.ProductionSystem;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.ProductionSystem.ShiftSchedulingManagement
{
    /// <summary>
    /// 班组设置services
    /// </summary>
    public class TeamServices: ApplicationService, ITeamServices
    {
        /// <summary>
        /// 班组仓储
        /// </summary>
        private readonly IRepository<TeamModel, int> _teamServices;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="teamServices">班组仓储</param>
        public TeamServices(IRepository<TeamModel, int> teamServices)
        {
            _teamServices = teamServices;
        }

        /// <summary>
        /// 添加班组
        /// </summary>
        /// <param name="dto">班组信息</param>
        /// <returns></returns>
        public async Task<int> CreateTeam(TeanListDto dto)
        {
            var entity = ObjectMapper.Map<TeanListDto, TeamModel>(dto);
            var result = await _teamServices.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 查询班组（分页/条件）
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns></returns>
        public async Task<List<TeanListDto>> QueryTeam(GetTeamDto dto)
        {
            var teamList = await _teamServices.GetListAsync();
            
            // 班组编号查询
            teamList = teamList.WhereIf(!string.IsNullOrEmpty(dto.TeamNumber), t => t.TeamNumber.Contains(dto.TeamNumber)).ToList();
            
            // 班组名称查询
            teamList = teamList.WhereIf(!string.IsNullOrEmpty(dto.TeamName), t => t.TeamName.Contains(dto.TeamName)).ToList();
            
            // 班组类型查询
            teamList = teamList.WhereIf(!string.IsNullOrEmpty(dto.TeamType), t => t.TeamType.Contains(dto.TeamType)).ToList();

            return ObjectMapper.Map<List<TeamModel>, List<TeanListDto>>(teamList);
        }

        /// <summary>
        /// 修改班组
        /// </summary>
        /// <param name="dto">班组信息</param>
        /// <returns></returns>
        public async Task<int> UpdateTeam(TeanListDto dto)
        {
            var entity = ObjectMapper.Map<TeanListDto, TeamModel>(dto);
            var result = await _teamServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除班组
        /// </summary>
        /// <param name="ids">班组ID列表</param>
        /// <returns></returns>
        public async Task BatchDeleteTeam(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return;
            await _teamServices.DeleteDirectAsync(t => ids.Contains(t.Id));
        }

        /// <summary>
        /// 根据ID获取班组信息
        /// </summary>
        /// <param name="id">班组ID</param>
        /// <returns></returns>
        public async Task<TeanListDto> GetTeamById(int id)
        {
            var entity = await _teamServices.GetAsync(id);
            return ObjectMapper.Map<TeamModel, TeanListDto>(entity);
        }
    }
}
